package gobelinmaker.client;

/*
 * JFileChooserOverwriteDialog.java
 *
 * Created on 11 November 2004, 10:51
 */

import java.io.File;

import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import javax.swing.filechooser.FileSystemView;

/**
 * A JFileChooser that takes into account instances when the user chooses
 * to save over an existing file name.
 *
 * @author  eugene
 */
public class JFileChooserOverwriteDialog extends JFileChooser {
    /**
     * Creates a new instance of JFileChooserOverwriteDialog with the default home directory selected
     */
    public JFileChooserOverwriteDialog() {
        super();
    }
    
    /**
     * Creates a new instance of JFileChooserOverwriteDialog with the specified directory selected
     * @param currentDirectory          The selected directory
     */
    public JFileChooserOverwriteDialog(File currentDirectory) {
        super(currentDirectory);
    }
    
    /**
     * Creates a new instance of JFileChooserOverwriteDialog with the specified directory selected and the given {@link FileSystemView}
     * @param currentDirectory      The selected directory
     * @param fsv                   The specified {@link FileSystemView}
     */
    public JFileChooserOverwriteDialog(File currentDirectory, FileSystemView fsv) {
        super(currentDirectory, fsv);
    }
    
    /**
     * Creates a new instance of JFileChooserOverwriteDialog with the specified directory selected
     * @param currentDirectoryPath          The selected directory
     */
    public JFileChooserOverwriteDialog(String currentDirectoryPath) {
        super(currentDirectoryPath);
    }
    
    /**
     * Creates a new instance of JFileChooserOverwriteDialog with the specified directory selected and the given {@link FileSystemView}
     * @param currentDirectoryPath      The selected directory
     * @param fsv                       The specified {@link FileSystemView}
     */
    public JFileChooserOverwriteDialog(String currentDirectoryPath, FileSystemView fsv) {
        super(currentDirectoryPath, fsv);
    }
    
    /**
     * Creates a new instance of JFileChooserOverwriteDialog with the given {@link FileSystemView}
     * @param fsv                   The specified {@link FileSystemView}
     */
    public JFileChooserOverwriteDialog(FileSystemView fsv) {
        super(fsv);
    }
    
    /**
     * If a the dialog is in a save mode, and the selected file already exists,
     * prompt the user whether to overwrite the existing file.
     */
    @Override
    public void approveSelection() {
        if (getDialogType() == SAVE_DIALOG) {
            File selectedFile = getSelectedFile();
            if (selectedFile.exists()) {
                Object[] o = {"Igen", "Nem"};
                int result = JOptionPane.showOptionDialog(
                        SwingUtilities.getWindowAncestor(this), 
                        "A fájl " + 
                                selectedFile.getAbsolutePath() + 
                                " már létezik."
                                + System.getProperty("line.separator") + 
                                "Szeretné felülírni a megadott fájlt?", 
                        "Fájl felülírása" , 
                        JOptionPane.YES_NO_OPTION, 
                        JOptionPane.WARNING_MESSAGE,
                        null, o, o[0]);
                if (result == JOptionPane.NO_OPTION) {
                    return;
                }
            }
        }
        super.approveSelection();
    }
}